package com.tju.resource.oauth.controller.menu;

import com.tju.resource.business.log.LogProcessor;
import com.tju.resource.common.ServiceException;
import com.tju.resource.model.log.LoginLogTool;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.business.sys.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author CLS
 * @Date 2022/1/5 18:21
 */
@RestController
public class HomeController {

    private static final String prefix = "home";

    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private LogProcessor logProcessor;
    @Autowired
    private ChineseExceptionUtils zh_ch;

    @PostMapping(prefix + "/sys/power/menu")
    public Result<Object> sysPowerMenu(@RequestBody ClientParameters permission) {
        if (permission.getUgc() != null) {
            return permissionService.getMenusByUserName(permission);
        } else
            return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }

    /**
     * 登录
     *
     * @param request
     * @param log
     * @return
     */
    @PostMapping(prefix + "/logIn")
    public Result<Object> logIn(HttpServletRequest request, @RequestBody ClientParameters.Log parameterslog) {
        try {
            if (parameterslog.getUserName() != null) {
                logProcessor.writeLogLog(new LoginLogTool(parameterslog.getUserName(), 0, "登录成功"), request);
                return ResultUtil.success(ResultEnum.SUCCESS);
            }
            throw new ServiceException(zh_ch.AbnormalOperation);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     *写入操作日志
     */
    //logProcessor.writeOperationLog(new OperationLogTool("岗位管理","新增岗位",0,log.getUserName(),"11111111111111111111111",HomeController.class.getName()), request);


    /**
     * 退出
     *
     * @param request
     * @param log
     * @return
     */
    @PostMapping(prefix + "/logOut")
    public Result<Object> logOut(HttpServletRequest request, @RequestBody ClientParameters.Log log) {
        try {
            if (log.getUserName() != null) {
                logProcessor.writeLogLog(new LoginLogTool(log.getUserName(), 0, "退出登录"), request);
                return ResultUtil.success(ResultEnum.SUCCESS);
            }
            throw new ServiceException(zh_ch.AbnormalOperation);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }
}
